<template>
    <div>
        <van-nav-bar
        title="修改密码"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
        />
        <div class="edit">
      <form action>
        <van-cell-group>
          <van-field
            v-model="password.oldpassword"
            type="password"
            label="旧密码"
            placeholder="请输入旧密码"
            :error-message="pwd_err"
            left-icon="closed-eye"
          />
          <van-field
            v-model="password.newpassword"
            type="password"
            label="新密码"
            placeholder="请输入新密码"
            left-icon="closed-eye"
            autocomplete="‘new-password’"
          />
          <van-field
            v-model="password.newpwd"
            type="password"
            label="新密码"
            placeholder="请确认新密码"
            :error-message="newpwd_err"
            left-icon="closed-eye"
            autocomplete="‘new-password’"
          />
        </van-cell-group>
      </form>
      <van-button color="red" size="large" @touchstart="reg">确认</van-button>
    </div>
    </div>
</template>
<script>
import { Toast } from "vant";
export default {
    data() {
    return {
      password: {
        oldpassword: "",
        newpassword: "",
        newpwd:''
      },
      newpwd_err: "",
      pwd_err: ""
    };
  },
  mounted() {
    this.m_title=this.$route.query.m_title;
      this.o_id=this.$route.query.o_id;
      let username= sessionStorage.getItem('username');
            if(!username){
              var that=this;
                Toast({
                message:'请先登录',
                duration:1000,
                onClose:function(){
                    that.$router.push('/login');
                }
                });
            }
  },
    methods: {
         onClickLeft() {
      this.$router.go(-1);
    },
    reg() {
      let oldpassword= this.password.oldpassword;
      let newpassword= this.password.newpassword;
      let newpwd=this.password.newpwd
      let password=sessionStorage.getItem('password');
      let username=sessionStorage.getItem('username');
      if (oldpassword!=password) {
        this.pwd_err = "密码输入错误";
        return false;
      }
      if (newpassword.length < 6) {
        this.newpwd_err = "密码长度必须大于6位";
      }
       if (newpwd!=newpassword) {
        this.newpwd_err = "两次密码输入不一致";
        return false;
      }
      this.pwd_err = this.newpwd_err = "";
      this.axios.post(
          "http://www.shop.com:8080/api.php/common/passwordEdit?access_token=5b7f60aca7e7f6f8c680b1b219ad3ec6&&old_password=" +
          oldpassword +
          "&&new_password=" +
          newpassword+
          "&&username="+username)
          .then(res => {

          if (res.data.status == 1) {
            Toast({
              message: "修改密码成功，请重新登录",
              icon: "success"
            });
            // 登录成功跳转到个人中心
            this.$router.push("/login")
          }else{
          Toast({
            message: res.data.msg,
            icon: "fail"
          });
         }
      });
    }
  }
}
</script>
<style scoped>

</style>
